package com.free.coder.common;

import java.util.ArrayList;
import java.util.List;

 
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 

import com.free.coder.log.XFLogger;
 

 
public abstract class HibernateCommonDao extends HibernateDaoSupport {
	//private static final Log log = LogFactory.getLog(HibernateCommonDao.class);

	public Session getCurSession(){
		return this.getSession();
	}
	
	public List findAll() {
	//	log.debug("finding all Userinfo instances");
		try {
			String queryString = "from Userinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
		//log.error("find all failed", re);
			throw re;
		}
	}

	public void save(Object obj) {
		//log.debug("saving Userinfo instance");
		try {
			
			getHibernateTemplate().save(obj);
		//	log.debug("save successful");
		} catch (RuntimeException re) {
		//	log.error("save failed", re);
			throw re;
		}
	}

	public void saveOrUpdate(Object obj) {
		//log.debug("saving Userinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(obj);
		//	log.debug("save successful");
		} catch (RuntimeException re) {
		//	log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(Object obj) {
		//log.debug("update Userinfo instance");
		try {
			getHibernateTemplate().update(obj);
			//log.debug("update successful");
		} catch (RuntimeException re) {
			//log.error("update failed", re);
			throw re;
		}
	}

	public void delete(Object obj) {
		//log.debug("deleting Userinfo instance");
		try {
			getHibernateTemplate().delete(obj);
			//log.debug("delete successful");
		} catch (RuntimeException re) {
		//	log.error("delete failed", re);
			throw re;
		}
	}

	public List findByHql(String hsql,Object value) {
		try {
			if(value == null){
				return getHibernateTemplate().find(hsql );
			}
			return getHibernateTemplate().find(hsql,value );
		} catch (RuntimeException re) {
		//	log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public Object findObjByHql(String hsql,Object value) {
		try {
			List list =null;
			if(value == null){
				list= getHibernateTemplate().find(hsql );
			}
			else{
				list= getHibernateTemplate().find(hsql,value );
			}
			if(list!=null&&list.size()>0){
				return list.get(0);
			}
			return null;
		} catch (RuntimeException re) {
		//	log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public void ExcuteSql(String hsql){
		try {
			  getHibernateTemplate().bulkUpdate(hsql);
		} catch (RuntimeException re) {
		//	log.error("find by property name failed", re);
			throw re;
		}
	}
	
	
	public String getSingerValue(String querystr) {
		String ret = "";
		try {
			List list = this.getHibernateTemplate().find(querystr);
			XFLogger.logger.info("list:" + list);
			if (list != null && list.size() > 0) {
				ret = list.get(0).toString();
			}

		} catch (Exception ex) {
			XFLogger.logger.error( ex.getMessage());
		}
		return ret;
	}

}
